package com.weilus.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

@Controller
@RequestMapping("admin/api")
public abstract class AdminController {
    public static ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

    public static <T> ConstraintViolation<T> validate(T bean) {
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<T>> constraintViolations = validator.validate(bean);
        if (!constraintViolations.isEmpty()) {
            ConstraintViolation cons = (ConstraintViolation) constraintViolations.toArray()[0];
            return cons;
        }
        return null;
    }

}
